# TODO: Matplotlib库笔记
# DATE: 2022/3/29
# AUTHOR: Cheng Ze WUST

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号

#绘制坐标点(1,1)(2,4)(3,9)...
plt.title('绘制1')
plt.plot([1,2,3,4,5],[1,4,9,16,25],'-.',color='r')
plt.xlabel('xlabel',fontsize=16)    #设置x轴标签，字体大小16
plt.ylabel('ylable')
plt.show()
# plt.clf()

#查看风格列表
print(plt.style.available)
plt.style.use('seaborn')    #选定风格预设
x=np.linspace(-10,10)   #以浮点数均匀步长生成数字序列
y=np.sin(x)
plt.plot(x,y)
plt.title('Fig2')
plt.show()

#region 手绘风格
# plt.xkcd()
# plt.plot(x,y)
# plt.show()
#endregion

#region 柱状图
np.random.seed(0)
x=np.arange(5)
y=np.random.randn(5)    #randn返回一个或一组样本，具有标准正态分布
fig,axes=plt.subplots()
v_bars=axes.bar(x,y,color='blue')
# 生成两个柱状图
# fig,axes=plt.subplots(ncols=2)
# v_bars=axes[0].bar(x,y,color='blue')
# h_bars=axes[1].barh(x,y,color='blue') #横向排列
plt.title('Fig3')
plt.show()

#带负数的柱状图
y=np.random.randint(-5,5,5) #randint返回一组随机整数，0-5之间，生成5个
print(y)
fig,axes=plt.subplots(ncols=1)
v_bars=axes.bar(x,y,color='blue')
axes.axhline(0,color='grey',linewidth=2)    #在0处加一条灰色宽度2的线
plt.title('Fig4')
plt.show()

#设置小于0的柱状样式
fig,axes=plt.subplots()
v_bars=axes.bar(x,y,color='lightblue')
for bar,height in zip(v_bars,y):
    if height<0:
        bar.set(color='green',edgecolor="black")
plt.title('Fig5')
plt.show()


x=np.random.randn(100).cumsum() #.cumsum()生成序列并累加
y=np.linspace(0,10,100)
fig,ax=plt.subplots()
ax.fill_between(x,y,color='grey')   #填充
plt.title('Fig6')
plt.show()
#endregion

#region 带有误差棒
mean_values=[1,2,3]
variance=[0.2,0.4,0.5]
bar_lable=['bar1','bar2','bar3']
x_pos=list(range(len(bar_lable)))
plt.bar(x_pos,mean_values,yerr=variance)
max_y=max(zip(mean_values,variance))
plt.ylim([0,(max_y[0]+max_y[1])*1.2])   #限制轴的范围
plt.ylabel('variable y')
plt.xticks(x_pos,bar_lable)
plt.title('Fig7')
plt.show()
#endregion

#region
x1=np.array([1,2,3])
x2=np.array([2,2,3])
bar_lables=['bar1','bar2','bar3']
fig=plt.figure(figsize=(8,6))
y_pos=np.arange(len(x1))
y_pos=[x for x in y_pos]
plt.barh(y_pos,x1,color='g',alpha=0.2)  #.bar是竖着 alpha为百分比透明度
plt.barh(y_pos,-x1,color='b',alpha=0.8)
plt.title('Fig8')
plt.show()
#endregion

#region
green_data=[1,2,3]
blue_data=[3,2,1]
red_data=[2,1,3]
labels=['group1','group2','group3']
pos=list(range(len(green_data)))
width=0.2
fig,ax=plt.subplots(figsize=(8,6))
plt.bar(pos,green_data,width,alpha=0.5,color='g',label=labels[0])
plt.bar([p+width for p in pos],blue_data,width,alpha=0.5,color='b',label=labels[1])
plt.bar([p+width*2 for p in pos],red_data,width,alpha=0.5,color='r',label=labels[2])
plt.title('Fig9')
plt.show()
#endregion

#region
data=range(200,225,5)
bar_lables=['a','b','c','d','e']
fig=plt.figure(figsize=(10,8))
y_pos=np.arange(len(data))
plt.yticks(y_pos,bar_lables,fontsize=16)
bars=plt.barh(y_pos,data,alpha=0.5,color='g')
plt.vlines(min(data),-1,len(data)+0.5,color='grey',alpha=0.3)
for b,d in zip(bars,data):
    plt.text(b.get_width()+b.get_width()*0.05,b.get_y()+b.get_height()/2,'{0:.2%}'.format(d/min(data)))
plt.title('Fig10')
plt.show()
#endregion

